// utils/page-mixin.js
const bubbleMixin = {
  data: {
    bubbleNotification: {
      show: false,
      title: '',
      content: '',
      time: '',
      message: null
    },
    bubbleTimer: null
  },

  // 显示气泡通知
  showBubbleNotification(options) {
    // 清除之前的定时器
    if (this.data.bubbleTimer) {
      clearTimeout(this.data.bubbleTimer)
    }

    const { title, content, message, duration = 3000 } = options
    
    this.setData({
      bubbleNotification: {
        show: true,
        title,
        content: content.length > 50 ? content.substring(0, 50) + '...' : content,
        time: this.formatTime(new Date()),
        message
      }
    })

    // 自动隐藏
    const timer = setTimeout(() => {
      this.hideBubbleNotification()
    }, duration)

    this.setData({ bubbleTimer: timer })
  },

  // 隐藏气泡通知
  hideBubbleNotification() {
    this.setData({
      'bubbleNotification.show': false
    })
  },

  // 气泡点击事件
  onBubbleTap() {
    const message = this.data.bubbleNotification.message
    if (message) {
      const app = getApp()
      app.onBubbleNotificationTap(message)
      this.hideBubbleNotification()
    }
  },

  // 关闭按钮点击
  onCloseTap() {
    this.hideBubbleNotification()
  },

  // 格式化时间
  formatTime(date) {
    const hours = date.getHours().toString().padStart(2, '0')
    const minutes = date.getMinutes().toString().padStart(2, '0')
    return `${hours}:${minutes}`
  },

  onUnload() {
    // 页面卸载时清除定时器
    if (this.data.bubbleTimer) {
      clearTimeout(this.data.bubbleTimer)
    }
  }
}

module.exports = bubbleMixin